#include "mainwindow.h"
#include "ui_mainwindow.h"

//连接|断开
void MainWindow::on_btn_link_dl8000_clicked()
{
    if(LangureStu == 1)
    {
        LangureStr = "Connect";
    }
    else
    {
        LangureStr = "连  接";
    }
    if(ui->btn_link_dl8000->text() == LangureStr)
    {
        //下拉菜单控件失能
        ui->cbx_port_dl8000->setEnabled(false);
        LangureStr_Init_Btn_Unlink(ui->btn_link_dl8000);

        LangureStr_Init_Testing(ui->te_prin_dl8000);
        //设置串口名
        CPortName_dl8000 = ui->cbx_port_dl8000->currentText();
        if(!CPortName_dl8000.isEmpty())
        {
            QString PortName = CPortName_dl8000.split(":").at(0);
            Port_dl8000->setPortName(PortName);

            RecvStu_dl8000 = false;
            ConnStu_dl8000 = 1;

            //设置波特率
            Port_dl8000->setBaudRate(115200);
            //设置停止位
            Port_dl8000->setStopBits(QSerialPort::OneStop);
            //设置数据位数
            Port_dl8000->setDataBits(QSerialPort::Data8);
            //设置奇偶校验
            Port_dl8000->setParity(QSerialPort::NoParity);
            //设置流控制
            Port_dl8000->setFlowControl(QSerialPort::NoFlowControl);
			
            //打开串口
            if(!Port_dl8000->open(QIODevice::ReadWrite))
            {
                if(LangureStu == 1)
                {
                    LangureStr = "Unable to open serial port";
                }
                else
                {
                    LangureStr = "无法打开串口";
                }
                InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);

                on_btn_link_dl8000_clicked();
                return;
            }

            LastPortStu_dl8000 = PORT_STU_NULL;
            PortStu_dl8000 = PORT_STU_SYNC;
            SyncTimer_dl8000->start(1000);
        }
        else
        {
            if(ConnectStu)
            {
                on_btn_link_dl8000_clicked();
            }
            else
            {
                //
                ui->cbx_mtsn_write_dl8000->setCheckState(Qt::Unchecked);

                ui->cbx_port_dl8000->setEnabled(true);
                LangureStr_Init_Btn_Link(ui->btn_link_dl8000);

                // 20230211-1617 新增检测错误提示
                if(LangureStu == 1)
                {
                    LangureStr = "Detection failed, no ports can be connected!";
                }
                else
                {
                    LangureStr = "检测失败，没有可以连接的端口!";
                }
                InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
            }
            return;
        }
    }
    else
    {
        QByteArray LineStr;
        LineStr.clear();
        Port_dl8000_Send_Data(LineStr, SEND_PORT_DISC);

        ConnStu_dl8000 = 0;
        DevVer_dl8000 = 0;

        if(SyncTimer_dl8000->isActive())
            SyncTimer_dl8000->stop();
        if(WaitTimer_dl8000->isActive())
            WaitTimer_dl8000->stop();
        //关闭串口
        Port_dl8000->clear();
        Port_dl8000->close();

        PortStu_dl8000 = PORT_STU_NULL;
        LastPortStu_dl8000 = PORT_STU_NULL;

        Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
        //
        ui->cbx_mtsn_write_dl8000->setCheckState(Qt::Unchecked);
        ui->cbx_port_dl8000->setEnabled(true);
        LangureStr_Init_Btn_Link(ui->btn_link_dl8000);
		
		
		if(LangureStu == 1)
		{
			LangureStr = "Current equipment version number: unrecognized";
		}
		else
		{
			LangureStr = "当前设备版本号：未识别";
		}
		ui->lbl_ver_dl8000->setText(LangureStr);
    }
}

//读取盖板码
void MainWindow::on_btn_mtsn_read_dl8000_clicked()
{
    SyncTimer_dl8000->stop();
    Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    //发送读取盖板码请求
    QByteArray LineStr;
    LineStr.clear();
    LastPortStu_dl8000 = PORT_STU_READ_MTSN;
    PortStu_dl8000 = PORT_STU_WAIT;
	//
    Port_dl8000_Send_3Time(LineStr, PORT_STU_READ_MTSN, SEND_READ_MTSN_REQ);
    if(PORT_STU_READ_MTSN != PortStu_dl8000)
    {
        if(PORT_STU_BUSY == PortStu_dl8000)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device is busy, please operate later!";
            }
            else
            {
                LangureStr = "设备繁忙，请稍后操作!";
            }
            InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else if(PORT_STU_TIMEOUT == PortStu_dl8000)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device timed out. Please check the device connection status and try again!";
            }
            else
            {
                LangureStr = "设备超时，请检查设备连接状态后重试!";
            }
            InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else
        {
            if(LangureStu == 1)
            {
                LangureStr = "Failed to read MtSN!";
            }
            else
            {
                LangureStr = "读取盖板码失败!";
            }
            InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);

        SyncTimer_dl8000->start(TIMER_SYNC_TIME_OUT);
        return;
    }

    if(!RecvMtSN_dl8000.isEmpty())
    {
        if((RecvMtSN_dl8000.size() == 44 || RecvMtSN_dl8000.size() == 88))
        {
			if(LangureStu == 1)
			{
				LangureStr = tr("The MtSN reading is completed");
			}
			else
			{
				LangureStr = QString("盖板码读取完成");
			}
            InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
            RecvMtSN_dl8000.replace('\0',' ');
            if(ModelStu < Model_12)
            {
                if(LangureStu == 1)
                {
                    LangureStr = tr("MtSN: ") + RecvMtSN_dl8000;
                }
                else
                {
                    LangureStr = QString("盖板码: ") + RecvMtSN_dl8000;
                }
                InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
            }
            else
            {
                if(LangureStu == 1)
                {
                    LangureStr = tr("Drive IC MtSN: ") + RecvMtSN_dl8000.mid(0,44);
                }
                else
                {
                    LangureStr = QString("驱动IC盖板码: ") + RecvMtSN_dl8000.mid(0,44);
                }
                InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
                if(LangureStu == 1)
                {
                    LangureStr = tr("TP-IC MtSN: ") + RecvMtSN_dl8000.mid(44,44);
                }
                else
                {
                    LangureStr = QString("TP-IC 盖板码: ") + RecvMtSN_dl8000.mid(44,44);
                }
                InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
            }
        }
        else
        {
            if(LangureStu == 1)
            {
                LangureStr = "MtSN reading failed!";
            }
            else
            {
                LangureStr = "盖板码读取失败!";
            }
            InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
    }
    else
    {
        if(LangureStu == 1)
        {
            LangureStr = "MtSN reading failed!";
        }
        else
        {
            LangureStr = "盖板码读取失败!";
        }
        InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
    }

    Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    SyncTimer_dl8000->start(TIMER_SYNC_TIME_OUT);

    if(PORT_STU_NULL == PortStu_dl8000)
    {
		LangureStr_Init_Btn_Unlink(ui->btn_link_dl8000);
        on_btn_link_dl8000_clicked();
    }
}
//写入盖板码
void MainWindow::on_btn_mtsn_write_dl8000_clicked()
{
    SyncTimer_dl8000->stop();
    Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    if(Port_dl8000_Write_Mtsn(SEND_BURN_MTSN_REQ))
    {
        LangureStr_Init_BurnCode_Succeed(ui->te_prin_dl8000);
        Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer_dl8000->start(TIMER_SYNC_TIME_OUT);

    if(PORT_STU_NULL == PortStu_dl8000)
    {
		LangureStr_Init_Btn_Unlink(ui->btn_link_dl8000);
        on_btn_link_dl8000_clicked();
    }
}

//升级
void MainWindow::on_btn_upg_dl8000_clicked()
{
    SyncTimer_dl8000->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    //...
    if(LangureStu == 1)
    {
        LangureStr = "The upgrade data is being transferred. Please do not disconnect the device...";
    }
    else
    {
        LangureStr = "正在传输升级数据，请勿断开设备...";
    }
	InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    if(Port_dl8000_Upgrade_File())
    {
        Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
        PortStu = PORT_STU_FREE;
    }
    else
    {
        Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer_dl8000->start(TIMER_SYNC_TIME_OUT);

    if(PORT_STU_NULL == PortStu)
    {
		LangureStr_Init_Btn_Unlink(ui->btn_link_dl8000);
        on_btn_link_dl8000_clicked();
    }
}


//机型切换
void MainWindow::on_cbx_model_read_dl8000_activated(int index)
{
    ModelStu_dl8000 = index;
}

void MainWindow::on_cbx_mtsn_write_dl8000_clicked()
{
    bool CbxStu = ui->cbx_mtsn_write_dl8000->isChecked();
    ui->btn_mtsn_write_dl8000->setEnabled(CbxStu);
}

//锁定解锁状态分支
void MainWindow::Port_dl8000_Function_Lock_Or_Unlock(int EnabledStu)
{
    switch(EnabledStu) {
    case DISP_BTN_LOCK_ALL:{
        ui->btn_mtsn_read_dl8000->setEnabled(false);
        ui->btn_mtsn_write_dl8000->setEnabled(false);
        ui->cbx_mtsn_write_dl8000->setEnabled(false);
        ui->btn_upg_dl8000->setEnabled(false);
        ui->cbx_upg_dl8000->setEnabled(false);
        break;
    }
    case DISP_BTN_UNLOCK_ALL:
    case DISP_BTN_LOCK_RES:{
        //
        ui->btn_mtsn_read_dl8000->setEnabled(true);
        ui->btn_mtsn_write_dl8000->setEnabled(false);
        ui->cbx_mtsn_write_dl8000->setEnabled(true);
        ui->btn_upg_dl8000->setEnabled(false);
        ui->cbx_upg_dl8000->setEnabled(true);
        ui->cbx_upg_dl8000->setCheckState(Qt::Unchecked);
        //
        ui->cbx_mtsn_write_dl8000->setCheckState(Qt::Unchecked);
        break;
    }
    }
}

